<?php 
/**
 *  Por Federico Pisarello - SisDev Software (c) 2012 - Buenos Aires, Argentina.
 * 	fpisarello@sisdevsoft.com
 */

require("includes/funciones.inc.php");

session_start();
if(!isset($_SESSION['idUser']))
	exit();

function searchABMClinical()
{
	require("includes/constants.php");
	require("includes/selectLanguage.php");
	require("includes/datatables_db/Datatables.php");
	
	$datatables = new Datatables();
	
	$datatables
			->select('c.idClinical, h.name, c.date AS clinicalDate')
			->from('clinical c')
			->join('horses h','c.idHorse = h.idHorse')
			->edit_column('clinicalDate','$1','formatoFecha(clinicalDate)')
			->where("c.state <> '".$stateDeleted."'");
	
	if($_SESSION['idTypeUser'] == $idTypeUser)
		$datatables->where("c.idHorse IN (SELECT idHorse FROM horses WHERE idUser = " . $_SESSION['idUser'] . ")");

	return $datatables->generate();	
}

function addOrEdit()
{
	require("includes/constants.php");
	require("includes/selectLanguage.php");
	$mcrypt = new MCrypt();
	
	$clinicalDate = date("Y-m-d", strtotime(str_replace("/", "-", $_REQUEST['ClinicalDate'])));

	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;

	$DB->StartTrans(); // start transaction
		
	if(!isset($_REQUEST["idClinical"])){			
		$strSQL = "INSERT INTO clinical ( ";
		$strSQL .= "	idHorse, ";
		$strSQL .= "	date, ";
		$strSQL .= "	RBC, ";
		$strSQL .= "	leukocytes, ";
		$strSQL .= "	hematocrit, ";
		$strSQL .= "	hemoglobin, ";
		$strSQL .= "	VCM, ";
		$strSQL .= "	HCM, ";
		$strSQL .= "	CHCM, ";
		$strSQL .= "	erythrocyteSedimentation, ";
		$strSQL .= "	fibrinogen, ";
		$strSQL .= "	bandNeutrophils, ";
		$strSQL .= "	segmentedNeutrophils, ";
		$strSQL .= "	eosinophils, ";
		$strSQL .= "	basophils, ";
		$strSQL .= "	lymphocytes, ";
		$strSQL .= "	monocytes, ";
		$strSQL .= "	totalProtein, ";
		$strSQL .= "	albumins, ";
		$strSQL .= "	globulins, ";
		$strSQL .= "	alb_glob, ";
		$strSQL .= "	totalBilirubin, ";
		$strSQL .= "	directBilirubin, ";
		$strSQL .= "	indirectBilirubin, ";
		$strSQL .= "	TGO, ";
		$strSQL .= "	CPK, ";
		$strSQL .= "	gammaGT, ";		
		$strSQL .= "	state ";
		$strSQL .= "	) VALUES ( ";
		$strSQL .= "	".$_REQUEST["cmbHorse"].", ";
		$strSQL .= "	'".$clinicalDate."', ";
		$strSQL .= "	'".$_REQUEST["RBC"]."', ";
		$strSQL .= "	'".$_REQUEST["Leukocytes"]."', ";
		$strSQL .= "	'".$_REQUEST["Hematocrit"]."', ";
		$strSQL .= "	'".$_REQUEST["Hemoglobin"]."', ";
		$strSQL .= "	'".$_REQUEST["VCM"]."', ";
		$strSQL .= "	'".$_REQUEST["HCM"]."', ";
		$strSQL .= "	'".$_REQUEST["CHCM"]."', ";
		$strSQL .= "	'".$_REQUEST["ErythrocyteSedimentation"]."', ";
		$strSQL .= "	'".$_REQUEST["Fibrinogen"]."', ";
		$strSQL .= "	'".$_REQUEST["BandNeutrophils"]."', ";
		$strSQL .= "	'".$_REQUEST["SegmentedNeutrophils"]."', ";
		$strSQL .= "	'".$_REQUEST["Eosinophils"]."', ";
		$strSQL .= "	'".$_REQUEST["Basophils"]."', ";
		$strSQL .= "	'".$_REQUEST["Lymphocytes"]."', ";
		$strSQL .= "	'".$_REQUEST["Monocytes"]."', ";
		$strSQL .= "	'".$_REQUEST["TotalProtein"]."', ";
		$strSQL .= "	'".$_REQUEST["Albumins"]."', ";
		$strSQL .= "	'".$_REQUEST["Globulins"]."', ";
		$strSQL .= "	'".$_REQUEST["Alb_Glob"]."', ";
		$strSQL .= "	'".$_REQUEST["TotalBilirubin"]."', ";
		$strSQL .= "	'".$_REQUEST["DirectBilirubin"]."', ";
		$strSQL .= "	'".$_REQUEST["IndirectBilirubin"]."', ";
		$strSQL .= "	'".$_REQUEST["TGO"]."', ";
		$strSQL .= "	'".$_REQUEST["CPK"]."', ";
		$strSQL .= "	'".$_REQUEST["GammaGT"]."', ";
		$strSQL .= "	'".$stateActive."' ";
		$strSQL .= "	)";
		
		$DB->Execute($strSQL);
	}
	else{
		$strSQL = "UPDATE clinical SET ";
		$strSQL .= "	idHorse = '".$_REQUEST["cmbHorse"]."', ";
		$strSQL .= "	date = '".$clinicalDate."', ";
		$strSQL .= "	RBC = '".$_REQUEST["RBC"]."', ";
		$strSQL .= "	leukocytes = '".$_REQUEST["Leukocytes"]."', ";
		$strSQL .= "	hematocrit = '".$_REQUEST["Hematocrit"]."', ";
		$strSQL .= "	hemoglobin = '".$_REQUEST["Hemoglobin"]."', ";
		$strSQL .= "	VCM = '".$_REQUEST["VCM"]."', ";
		$strSQL .= "	HCM = '".$_REQUEST["HCM"]."', ";
		$strSQL .= "	CHCM = '".$_REQUEST["CHCM"]."', ";
		$strSQL .= "	erythrocyteSedimentation = '".$_REQUEST["ErythrocyteSedimentation"]."', ";
		$strSQL .= "	fibrinogen = '".$_REQUEST["Fibrinogen"]."', ";
		$strSQL .= "	bandNeutrophils = '".$_REQUEST["BandNeutrophils"]."', ";
		$strSQL .= "	segmentedNeutrophils = '".$_REQUEST["SegmentedNeutrophils"]."', ";
		$strSQL .= "	eosinophils = '".$_REQUEST["Eosinophils"]."', ";
		$strSQL .= "	basophils = '".$_REQUEST["Basophils"]."', ";
		$strSQL .= "	lymphocytes = '".$_REQUEST["Lymphocytes"]."', ";
		$strSQL .= "	monocytes = '".$_REQUEST["Monocytes"]."', ";
		$strSQL .= "	totalProtein = '".$_REQUEST["TotalProtein"]."', ";
		$strSQL .= "	albumins = '".$_REQUEST["Albumins"]."', ";
		$strSQL .= "	globulins = '".$_REQUEST["Globulins"]."', ";
		$strSQL .= "	alb_glob = '".$_REQUEST["Alb_Glob"]."', ";
		$strSQL .= "	totalBilirubin = '".$_REQUEST["TotalBilirubin"]."', ";
		$strSQL .= "	directBilirubin = '".$_REQUEST["DirectBilirubin"]."', ";
		$strSQL .= "	indirectBilirubin = '".$_REQUEST["IndirectBilirubin"]."', ";
		$strSQL .= "	TGO = '".$_REQUEST["TGO"]."', ";
		$strSQL .= "	CPK = '".$_REQUEST["CPK"]."', ";
		$strSQL .= "	gammaGT = '".$_REQUEST["GammaGT"]."', ";
		$strSQL .= "	state = '".$stateActive."' ";
		$strSQL .= " WHERE idClinical = ". $_REQUEST["idClinical"];
		
		$DB->Execute($strSQL);
	}
	
	if (!$DB->CompleteTrans())
	{
		$arrJSON->status = "ERROR";
		$arrJSON->msg = $a_languages['Common.ErrorGrabar'];
	}
	else
	{
		$arrJSON->status = "OK";
		$arrJSON->msg = $a_languages['Common.OkGrabar'];
	}	
	return json_encode($arrJSON);
}

function del()
{
	require("includes/constants.php");
	require("includes/selectLanguage.php");
	
	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;
	
	$DB->StartTrans(); // start transaction
	$DB->Execute("UPDATE clinical SET state='$stateDeleted' WHERE idClinical = " . $_REQUEST["idClinical"]); // execute query

	if (!$DB->CompleteTrans())
	{
		$arrJSON->status = "ERROR";
		$arrJSON->msg = $a_languages['Common.ErrorEliminar'];
	}
	else
	{
		$arrJSON->status = "OK";
		$arrJSON->msg = $a_languages['Common.OkEliminar'];
	}	
	return json_encode($arrJSON);
}

function editClinical()
{
	require("includes/constants.php");
	
	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;
	
	$rsClinical = $DB->Execute("SELECT * FROM clinical WHERE state <> '$stateDeleted' AND idClinical = " . $_REQUEST["idClinical"]); // execute query
						
	if(!$rsClinical->EOF){
		
		$joClinical->data["idHorse"] = $rsClinical->fields("idHorse");
		$joClinical->data["clinicalDate"] = date("d/m/Y", strtotime($rsClinical->fields("date")));
		$joClinical->data["RBC"] = $rsClinical->fields("RBC");
		$joClinical->data["Leukocytes"] = $rsClinical->fields("leukocytes");
		$joClinical->data["Hematocrit"] = $rsClinical->fields("hematocrit");
		$joClinical->data["Hemoglobin"] = $rsClinical->fields("hemoglobin");
		$joClinical->data["VCM"] = $rsClinical->fields("VCM");
		$joClinical->data["HCM"] = $rsClinical->fields("HCM");
		$joClinical->data["CHCM"] = $rsClinical->fields("CHCM");
		$joClinical->data["ErythrocyteSedimentation"] = $rsClinical->fields("erythrocyteSedimentation");
		$joClinical->data["Fibrinogen"] = $rsClinical->fields("fibrinogen");
		$joClinical->data["BandNeutrophils"] = $rsClinical->fields("bandNeutrophils");
		$joClinical->data["SegmentedNeutrophils"] = $rsClinical->fields("segmentedNeutrophils");
		$joClinical->data["Eosinophils"] = $rsClinical->fields("eosinophils");
		$joClinical->data["Basophils"] = $rsClinical->fields("basophils");
		$joClinical->data["Lymphocytes"] = $rsClinical->fields("lymphocytes");
		$joClinical->data["Monocytes"] = $rsClinical->fields("monocytes");
		$joClinical->data["TotalProtein"] = $rsClinical->fields("totalProtein");
		$joClinical->data["Albumins"] = $rsClinical->fields("albumins");
		$joClinical->data["Globulins"] = $rsClinical->fields("globulins");
		$joClinical->data["Alb_Glob"] = $rsClinical->fields("alb_glob");
		$joClinical->data["TotalBilirubin"] = $rsClinical->fields("totalBilirubin");
		$joClinical->data["DirectBilirubin"] = $rsClinical->fields("directBilirubin");
		$joClinical->data["IndirectBilirubin"] = $rsClinical->fields("indirectBilirubin");
		$joClinical->data["TGO"] = $rsClinical->fields("TGO");
		$joClinical->data["CPK"] = $rsClinical->fields("CPK");
		$joClinical->data["GammaGT"] = $rsClinical->fields("gammaGT");
		
		return json_encode($joClinical->data);
	}
	else
		return json_encode('');
}

function getHorses()
{
	require("includes/constants.php");
	require("includes/selectLanguage.php");

	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;
	
	if($_SESSION['idTypeUser'] == $idTypeAdministrator)
		$sSQL = "SELECT * FROM horses";
	else 
		$sSQL = "SELECT * FROM horses WHERE idUser = " . $_SESSION['idUser'];

	$rsHorses = $DB->Execute($sSQL);

	$i=0;
	while(!$rsHorses->EOF){
		$arrHorses->data[$i]['idHorse'] = $rsHorses->fields(0);
		$arrHorses->data[$i++]['name'] = $rsHorses->fields(1);
		$rsHorses->MoveNext();
	}

	return json_encode($arrHorses->data);
}

switch($_REQUEST['actionOfForm'])
{
	case "EDIT":
		echo editClinical();
		break;
	case "DELETE":
		echo del();
		break;
	case "getHorses":
		echo getHorses();
		break;
	case "search":
		echo searchABMClinical();
		break;
	case "addOrEdit":
		echo addOrEdit();
		break;		
}
?>
